package com.tjsoft.system.util;



import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tjsoft.system.entity.base.User;


public class SecurityFilter implements Filter {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
//	private WebApplicationContext ctx ;
	
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse)arg1;
		String requestStr = req.getRequestURI();
//		String queryString = req.getQueryString();
		try {
			if(requestStr.endsWith("/app/sys/user/user!login")){
				arg2.doFilter(arg0, arg1);
				return ;
			}else{
				User user = (User)req.getSession().getAttribute("user");
				if(user==null){
					response.sendRedirect("/common/error.jsp");
				}else{
					arg2.doFilter(arg0, arg1);
					return ;
				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return;
	}

	public void init(FilterConfig filterConfig) throws ServletException {
//		ctx= WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext()  );
	}

	public void destroy() {
		
	}


}


